package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.ImporderMonitorBean;

import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * 订单获取监控选择店铺Adapter
 * Created by ShineF on 2017/9/21 0021.
 */

public class ImportOrdersMonitorRecycleAdapter extends RecyclerView.Adapter<ImportOrdersMonitorRecycleAdapter.ImportOrdersMonitorHolder> {


    private Context mContext;
    private List<ImporderMonitorBean.ListBean> mList;
    private int mHeight;
    private OnItemClickListener mListener;

    public ImportOrdersMonitorRecycleAdapter(List<ImporderMonitorBean.ListBean> list, int height, Context context) {
        mContext = context;
        mList = list;
        mHeight = height;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }


    @Override
    public ImportOrdersMonitorHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ImportOrdersMonitorHolder(View.inflate(mContext, R.layout.adapter_import_orders_monitor_recycle, null));
    }

    @Override
    public void onBindViewHolder(ImportOrdersMonitorHolder holder, final int position) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mItem.getLayoutParams();
        params.height = (mHeight - ConvertUtils.dip2px(mContext, 20)) / 3;
        ImporderMonitorBean.ListBean listBean = mList.get(position);
        holder.mTvShopName.setText(listBean.getName());
//        if (TextUtils.isEmpty(listBean.getPicUrl())) {
//            holder.mShopIcon.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.pic_not_found));
//        } else {
        ImageloaderUtils.displayImg(listBean.getPicUrl(), holder.mShopIcon);
//        }
        holder.mItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.ItemClickListener(position, view, mList.get(position));
                }
            }
        });
    }

    public interface OnItemClickListener {
        void ItemClickListener(int position, View view, ImporderMonitorBean.ListBean data);
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ImportOrdersMonitorHolder extends RecyclerView.ViewHolder {

        CircleImageView mShopIcon;
        TextView mTvShopName;
        LinearLayout mItem;

        ImportOrdersMonitorHolder(View itemView) {
            super(itemView);
            mShopIcon = (CircleImageView) itemView.findViewById(R.id.iv_monitor_shop_icon);
            mTvShopName = (TextView) itemView.findViewById(R.id.tv_shop_name);
            mItem = (LinearLayout) itemView.findViewById(R.id.ll_item);
        }
    }
}
